﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Chloe.Ext;
using Newtonsoft.Json.Linq;
namespace Ace
{
    public class Result<T> : Result, IJsonSerialize
    {
        public Result()
        {
        }
        public Result(ResultStatus status)
            : base(status)
        {
        }
        public Result(ResultStatus status, T data)
            : base(status)
        {
            this.Data = data;
        }
        public string toJsonString()
        {
            if (this.Data == null)
            {
                JObject jobj = new JObject();
                jobj.Add("Status", (int)this.Status);

                jobj.Add("Data", null);
                return jobj.ToString();
            }
            var x = BaseContextSpace.CsObjToJson(this.Data);
            if (x == null)
            {
                if (this.Data.GetType().IsAssignableFrom(typeof(JToken)))
                {
                    JObject jobj = new JObject();
                    jobj.Add("Status", (int)this.Status);

                    jobj.Add("Data", (this.Data as JToken));
                    return jobj.ToString();
                }
                else
                   if (this.Data.GetType().IsAssignableFrom(typeof(IJsonSerialize)))
                {
                    JObject jobj = new JObject();
                    jobj.Add("Status", (int)this.Status);

                    jobj.Add("Data", JObject.Parse((this.Data as IJsonSerialize).toJsonString()));
                    return jobj.ToString();

                }
                else
                return JsonHelper.Serialize(this);
            }
            else
            {
                JObject jobj = new JObject();
                jobj.Add("Status", (int)this.Status);

                jobj.Add("Data", x);
                return jobj.ToString();
            }
      }

        public T Data { get; set; }
    }
}
